var app = {

    init: function(){

        var self = this;

        $(document).scrollTop(0);

        // generator carousels
        $('.slideholder').slick({
            dots: true,
            cssEase: 'cubic-bezier(0.785, 0.135, 0.15, 0.86)',
            speed: 600,
            onBeforeChange: function(el, currentIndex, targetIndex){
                var bgColor = el.$list.find('.slide:eq('+ (targetIndex + 1) + ') img').data('color');
                el.$list.closest('.generator').css('background', bgColor);
            }
        });

        // case type anchor points
        $('.case-anchor').mouseover(function(e){
            var type = $(this).data('type');
            $('.many-ways .phone.' + type + ' img').css({
                '-moz-transform': 'translateY(-25px)',
                '-ms-transform': 'translateY(-25px)',
                '-webkit-transform': 'translateY(-25px)',
                'transform': 'translateY(-25px)'
            });
        });
        $('.case-anchor').mouseout(function(e){
            var type = $(this).data('type');
            $('.many-ways .phone.' + type + ' img').css({
               '-moz-transform': 'translateY(0px)',
                '-ms-transform': 'translateY(0px)',
                '-webkit-transform': 'translateY(0px)',
                'transform': 'translateY(0px)'
            });
        });


        var $fadebg = $('.fade-bg'), $footer = $('.footer');

        var h = $(window).height(), dh = $(document).height();

        $('a').click(function(){
            $('html, body').animate({
                scrollTop: $( $.attr(this, 'href') ).offset().top
            }, 500);
            return false;
        });

        $(window).scroll(function(){
            var s = $(window).scrollTop();
            if(s > 150){
                $('.top-bar').addClass('highlight');
            }else{
                $('.top-bar').removeClass('highlight');
            }
        });
    },

    resize: function(){
        var w = $(window).width(),
            h = $(window).height();
        $('.masthead').css({
            height: h
        });

        // $('.phone-arrangement').css({
        //     height: (h * 1.5) + 'px'
        // });
    },

    map_range : function(value, low1, high1, low2, high2){
        return low2 + (high2 - low2) * (value - low1) / (high1 - low1);
    },

    initialAnimation: function(){
        var l = $('.masthead .phone').length,
            delay = 0.10;
        
        TweenMax.to($('.preloader-cover'), 1, {opacity:0, onComplete: function(){
            $('.preloader-cover').remove();
        }});;

        $('.masthead .phone').each(function(i){
            TweenMax.to($(this), 1, {delay: (l - i) * delay, opacity:1});
            TweenMax.to($(this), 0.45, {delay: (l - i) * delay, x: 15, ease: Quad.easeOut});
        });

        var copyDelay = 1.1;
        TweenMax.to($('.masthead h1'), 1, {delay:copyDelay, opacity:1});
        TweenMax.to($('.masthead h2'), 1, {delay:copyDelay + 0.15, opacity:1});

        TweenMax.to($('.masthead h1'), 0.3, {delay: copyDelay, y:-15, ease: Quad.easeOut});
        TweenMax.to($('.masthead h2'), 0.3, {delay: copyDelay + 0.15, y:-15, ease: Quad.easeOut});
    }

}


$(function() {
    var timeout,
        isTimeoutReached = false,
        isImagesLoaded = false;

    timeout = setTimeout(function(){
        isTimeoutReached = true;
        checkForInit();
    }, 3000);

    $('.preload-me').imagesLoaded(function(){
        isImagesLoaded = true;
        checkForInit();
    });
    
    // checks to make sure timeout is reached and images are loaded
    function checkForInit(){
        if(isTimeoutReached && isImagesLoaded){
            clearTimeout(timeout);
            app.initialAnimation();
        }
    }

    app.init();
    $(window).resize($.proxy(app.resize, app));
    app.resize();
});

